﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PinLayoutMVC.Models;
using PinLayoutMVC.Core;

namespace PinLayoutMVC.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SurveyOverview()
        {
            return View(DummyData.GetData());
        }

        public ActionResult SurveyDetails(int id)
        {
            return View(DummyData.GetData().Surveys.Where(survey => survey.ID == id).First());
        }

        public ActionResult PrevQuestion(int id)
        {
            id--;

            return RedirectToAction("Question", new { id }); 
        }

        public ActionResult NextQuestion(int id)
        {
            id++;

            return RedirectToAction("Question", new { id });
        }

        [HttpGet]
        public ActionResult Question(int id)
        {
            QuestionModel question = DummyData.GetData().Surveys.FirstOrDefault().Questions.
                                        Where(q => q.ID == id).FirstOrDefault();
            //Dan maar even zo
            if (question == null)
                question = DummyData.GetData().Surveys.LastOrDefault().Questions.
                                        Where(q => q.ID == id).FirstOrDefault();

            return View(question);
        }

        [HttpGet]
        public ActionResult Answer(int id)
        {
            AnswerModel answer = DummyData.GetAnswerById(id);

            return View(answer);
        }

        [HttpPost]
        public ActionResult Answer(AnswerModel model)
        {
            AnswerModel answer = DummyData.GetAnswerById(model.ID);

            answer.Text = model.Text;
            answer.Number = model.Number;
            answer.LeadsToQuestion = model.LeadsToQuestion;

            return View(model);
        }

        [HttpPost]
        public ActionResult Question(QuestionModel model)
        {
            SurveyModel survey = DummyData.GetData().Surveys.Where(s => s.ID == model.SurveyID).FirstOrDefault();
            QuestionModel Question = survey.Questions.Where(q => q.ID == model.ID).FirstOrDefault();

            Question.Number = model.Number;
            Question.Question = model.Question;
            Question.Description = model.Description;
            Question.AllowFeedback = model.AllowFeedback;
            Question.FeedbackText = model.FeedbackText;
            Question.Type = model.Type;

            

            return View(model);
        }

        public ActionResult CreateNewQuestion(int id)
        {
            SurveyModel survey = DummyData.GetData().Surveys.Where(s => s.ID == id).FirstOrDefault();
            QuestionModel question = new QuestionModel();

            question.ID = DummyData.GetData().getQuestionId();
            question.SurveyID = id;

            survey.Questions.Add(question);

            AnswerModel answer = new AnswerModel();
            answer.ID = DummyData.GetData().getAnswerId();
            answer.Number = 1;
            answer.LeadsToQuestion = 0;

            question.Answers.Add(answer);

            return RedirectToAction("Question", new { question.ID});
        }

        [HttpGet]
        public ActionResult CreateNewSurvey()
        {
            SurveyModel survey = new SurveyModel();

            survey.ID = DummyData.GetData().Surveys.LastOrDefault().ID + 1;
            survey.Name = "new survey";
            survey.CreationDate = DateTime.Now;
            survey.Published = false;

            DummyData.GetData().Surveys.Add(survey);

            return View(survey);
        }

        [HttpPost]
        public ActionResult CreateNewSurvey(SurveyModel model)
        {
            SurveyModel survey = DummyData.GetData().Surveys.Where(s => s.ID == model.ID).FirstOrDefault();
            
            survey.Name = model.Name;
            survey.Description = model.Description;

            return RedirectToAction("SurveyOverview");
        }

        public ActionResult PublishSurvey(int id)
        {
            SurveyModel survey = DummyData.GetData().Surveys.Where(s => s.ID == id).FirstOrDefault();
            survey.ValidateAndPublish();
            return View("SurveyDetails", survey);
        }




    }
}
